<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="db431888431"; // Database name 

$dbLink = mysql_connect($host, $username, $password);
if (!$dbLink) die("Impossible d'établir une connexion sur la base de données: " . mysql_error());
$dbSelected = mysql_select_db($db_name, $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());

?>
<html>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="css/skin/css3-buttons-grey.css" type="text/css"  media="screen">
<link rel="stylesheet" href="css/css3-buttons.css" type="text/css"  media="screen">
<link rel="stylesheet" href="css/mysheet.css" type="text/css"  media="screen"><script>
function tableHighlightRow() {
  if (document.getElementById && document.createTextNode) {
    var tables=document.getElementsByTagName('table');
    for (var i=0;i<tables.length;i++)
    {
      if(tables[i].className=='hilite') {
        var trs=tables[i].getElementsByTagName('tr');
        for(var j=0;j<trs.length;j++)
{
          if(trs[j].parentNode.nodeName=='TBODY') {
            trs[j].onmouseover=function(){this.className='hilight';return false}
            trs[j].onmouseout=function(){this.className='';return false}
          }
        }
      }
    }
  }
}
window.onload=function(){tableHighlightRow();}
</script>



<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);

$today = $_POST["date"];
$parcoursName = $_POST["parcoursname"];
$idparcours = $_POST["parcoursid"]; 

?>

<table width="80%" border="1" align="right">
<thead>
<tr>
	<?php
	echo "<th>" . $today . "</th>";
	$query 		= 'SELECT id, name FROM courses where id =' . $idparcours;
	$result 	= mysql_query($query);
	if(mysql_num_rows($result) ){
		while($row = mysql_fetch_array($result))  {
			$monParcours = $row['name'];
		}	
	}
	echo "<th colspan=\"19\">" . $parcoursName . "</th>\n";
	echo "<input style =\"text-align:center\" type=\"hidden\" readonly=\"true\" name=\"parcoursid\" size=\"60\" value=\"" . $monParcours . "\"></th>\n";
?>
</tr>
</thead>

<tr>
<td>Par</td>
<?php

$query 		= 'SELECT num, par FROM courses_hole WHERE id_course = "' . $idparcours . '" order by num;';
echo $query;
$result 	= mysql_query($query);

if(mysql_num_rows($result) ){
	while($row = mysql_fetch_array($result))  {
		echo "<td><input style =\"text-align:center\"  type=\"textbox\" value =\"" . $row['par'] . "\" size=\"1\" readonly> </td>\n";
	
	}	
}

else {
	echo "<td colspan=\"19\">aucun parcours trouvé.</td>";
}
?>
</tr>

<tr>
<td>Score</td>
<?php
$nbTrous = count($_POST["S"]);    
for ($i = 0; $i < $nbTrous; $i++)    {
	//echo "<td>" . $_POST["S"][$i] . "</td>\n";
	echo "<td><input style =\"text-align:center\"  type=\"textbox\" value =\"" . $_POST["S"][$i] . "\" size=\"1\" readonly> </td>\n";
}
?>
</tr>

<tr>
<td>AP</td>
<?php
$i = 0;
$j = 0;
$nbTrous = count($_POST["S"]);  
$nbAP =  count($_POST["AP"]);
for ($i = 1; $i < $nbTrous+1; $i++)    {
	if ($_POST["AP"][$j] == $i) { 
		echo "<td><input type=\"checkbox\" checked=\"checked\"></td>\n";
		$j++;
		}
		else {
			echo "<td><input type=\"checkbox\"></td>\n";
		}
}
?>
</tr>



<tr>
<td>Putts</td>
<?php
$nbTrous = count($_POST["P"]);    
for ($i = 0; $i < $nbTrous; $i++)    {
	//echo "<td>" . $_POST["P"][$i] . "</td>\n";
	echo "<td><input style =\"text-align:center\"  type=\"textbox\" value =\"" . $_POST["P"][$i] . "\" size=\"1\" readonly> </td>\n";
}
?>
</tr>

</table>

<?php
// on insert en base la carte
$j = 0;
$apvalue = 0; // a determiner si approche putt
$gir = 0; // a determiner si GIR
$nbTrous = count($_POST["P"]);  
  
for ($i = 0; $i < $nbTrous; $i++)    {
	//on determine si il y a eu une approche putt avec succes
	if ($_POST["AP"][$j] == $i) { 
			$apvalue = 1;
			$j++;
		}
		else {
			$apvalue = 0;
		}
	// on determine si il y a un GIR avec succes
	if ($_POST["PAR"][$i] - $_POST["P"]$i >= 2) {
		$gir = 1; // reussi
	}
	else {
		$gir = 0; // manque
	}
		
	// on execute la requete d'enregistrement
	$query = "INSERT INTO events_score (`id_events`, `id`, `idcourse`, `numhole`, `fw`, `gir`, `ap`, `putts`, `score`) VALUES ('123', NULL, '" .$idparcours ."', '" .$i . "', '0', '" . $gir . "', '" . $apvalue . "', '" . $_POST["P"][$i] . "', '" . $_POST["S"][$i] . "');"
	mysql_query($query);

}	
?>

</body>
</html>